﻿using AutoMapper;
using MyCompany.MyProject.Common;
using MyCompany.MyProject.IService;
using MyCompany.MyProject.Model.Models;
using MyCompany.MyProject.Repository;
using MyCompany.MyProject.Repository.Base;

namespace MyCompany.MyProject.Service
{
    public class UserService : BaseService<SysUserInfo>, IUserService
    {
        private readonly IDepartmentService departmentService;
        private readonly IBaseRepository<Role> roleRepository;
        private readonly IBaseRepository<UserRole> userRoleRepository;
        private readonly IUserRepository userRepository;

        public UserService(/*IMapper mapper,*/ IBaseRepository<SysUserInfo> baseRepository, IDepartmentService departmentService,
            IBaseRepository<Role> roleRepository,
            IBaseRepository<UserRole> userRoleRepository,
            IUserRepository userRepository) : base(baseRepository)
        {
            this.departmentService = departmentService;
            this.roleRepository = roleRepository;
            this.userRoleRepository = userRoleRepository;
            this.userRepository = userRepository;
        }

        /// <summary>
        /// 获取用户角色名称字符串
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<string> GetUserRoleNameStr(string loginName, string password)
        {
            string roleName = "";
            var user = (await Query(a => a.LoginName == loginName && a.LoginPWD == password)).FirstOrDefault();
            var roleList = await roleRepository.Query(a => a.IsDeleted == false);
            if (user != null)
            {
                var userRoles = await userRoleRepository.Query(u => u.UserId == user.Id);
                if (userRoles.Count() > 0)
                {
                    var arr = userRoles.Select(u => u.RoleId.ObjToString()).ToList();
                    var roles = roleList.Where(d => arr.Contains(d.Id.ObjToString()));

                    roleName = string.Join(",", roles.Select(r => r.Name).ToArray());
                }
            }
            return roleName;
        }

        public async Task<List<RoleModulePermission>> RoleModuleMaps()
        {
            return await userRepository.RoleModuleMaps();
        }

        //public async Task<List<UserVo>> Query()
        //{
        //    var userRepo = new UserRepository();
        //    var users = await userRepo.Query();
        //    return users.Select(d=>new UserVo { UserName = d.Name}).ToList();
        //}

        [UseTran(Propagation = Propagation.Required)]
        public async Task<bool> TestTranPropagation()
        {
            await Console.Out.WriteLineAsync($"db context id : {base.Db.ContextID}");
            var sysUserInfos = await base.Query();

            TimeSpan timeSpan = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var id = timeSpan.TotalSeconds.ObjToLong();
            var insertSysUserInfo = await base.Add(new SysUserInfo()
            {
                Id = id,
                Name = $"user name {id}",
                Status = 0,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                CriticalModifyTime = DateTime.Now,
                LastErrorTime = DateTime.Now,
                ErrorCount = 0,
                Enable = true,
                TenantId = 0,
            });

            await departmentService.TestTranPropagation();

            return true;
        }


    }
}
